package net.test.piggy.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitClientConfig {
    @Bean
    public DirectExchange piggyDirectExchange(){
        return new DirectExchange("piggy.direct.exchange");
    }

    @Bean
    public Queue piggyQueue(){
        return new Queue("piggy-direct-queue");
    }

    @Bean
    public Queue tempQueue(){
        return new Queue("temp-direct-queue");
    }

    @Bean
    public Binding piggyBinding(){
        return BindingBuilder.bind(piggyQueue()).to(piggyDirectExchange()).with("piggy_key");
    }

    @Bean
    public Binding tempBinding(){
        return BindingBuilder.bind(tempQueue()).to(piggyDirectExchange()).with("temp_key");
    }

    @Bean
    public Queue ttlQueue(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange","piggy.dead.letter.exchange");
        args.put("x-dead-letter-routing-key","dead_letter");
        // ms
        args.put("x-message-ttl",10000);
        return new Queue("ttl-queue",true,false,false, args);
    }

    @Bean
    public Exchange ttlExchange(){
        return new DirectExchange("piggy.ttl.exchange");
    }

    @Bean
    public Binding ttlBinding(){
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with("ttl_key").noargs();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }
}
